package web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import beans.User;


import dao.UserDAO;
import enums.Cities;
import enums.Countries;

@Controller
public class UsersController {
	private UserDAO userDAO;

	public void setUserDAO(UserDAO userDAO) {
		this.userDAO = userDAO;
	}
	@RequestMapping(value = "/deleteuser={username}", method=RequestMethod.GET)
	public ModelAndView uzytkownicy(@PathVariable("username") String username, HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		User user = userDAO.findUserByUsername(username);
		userDAO.deleteUser(user);
		return new ModelAndView("redirect:uzytkownicy.htm");
	}
	@RequestMapping(value = "/edituser={username}", method=RequestMethod.GET)
	public ModelAndView edituser(@PathVariable("username") String username, HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		User editedUser = userDAO.findUserByUsername(username);
		ModelMap modelMap = new ModelMap();
		modelMap.addAttribute("editedUser", editedUser);
		modelMap.addAttribute("cities",Cities.values());
		modelMap.addAttribute("countries", Countries.values());
		return new ModelAndView("edituser", modelMap);
	}
	@RequestMapping(value = "/adminuseredit={username}", method=RequestMethod.POST)
	public ModelAndView adminuseredit(@PathVariable("username") String username, HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		String name = request.getParameter("name");
		String email = request.getParameter("email");
		String telephone = request.getParameter("telephone");
		String street = request.getParameter("street");
		String city = request.getParameter("city");
		String country = request.getParameter("country");
		User user = userDAO.findUserByUsername(username);
		user.setName(name);
		user.setEmail(email);
		user.setTelephone(telephone);
		user.setStreet(street);
		user.setCity(city);
		user.setCountry(country);
		userDAO.UpdateUser(user);
		return new ModelAndView("redirect:uzytkownicy.htm");
	}
	
}
